﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Afvanding.RefToPumpService;

namespace Afvanding
{
    /// <summary>
    /// Interaction logic for StationPropertiesWindow.xaml
    /// </summary>
    public partial class StationPropertiesWindow : Window
    {
        private readonly int _stationId;
        private readonly PumpServiceClient _pumpService;
        private readonly Station _station;

        public StationPropertiesWindow(int stationId)
        {
            InitializeComponent();

            _stationId = stationId;
            _pumpService = new PumpServiceClient();

            // Set station info in GUI:
            _station = _pumpService.FindStation(_stationId);
            lblStationId.Content = "Station: " + _station.Id + " Properties";
            txtIPaddress.Text = _station.IPAdresse;
            txtPort.Text = _station.Port.ToString();
            txtWaterLevel.Text = _station.WaterLevel.ToString();
            cbStatus.SelectedIndex = _station.Status == 1 ? 0 : 1;
            
            // Get pumps info in GUI:
            dgPumps.ItemsSource = _pumpService.RequestCurrentStatus(_stationId);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Station
            int port;
            double waterLevel;
            if (double.TryParse(txtWaterLevel.Text, out waterLevel) &&
                int.TryParse(txtPort.Text, out port))
            {
                int status = int.MinValue;

                if (cbStatus.Text == "Running")
                    status = 1;
                else if (cbStatus.Text == "Stopped")
                    status = 0;

                if (_pumpService.EditStation(_stationId, txtIPaddress.Text, port, waterLevel, status))
                    MessageBox.Show("Station: " + _stationId + " updated.");
                else
                    MessageBox.Show("An error occured");
            }

            // Pumps:
            dgPumps.CommitEdit(DataGridEditingUnit.Row, true);

            for (int i = 0; i < dgPumps.Items.Count - 1; i++)
            {
                var row = (DataGridRow) dgPumps.ItemContainerGenerator.ContainerFromIndex(i);
                var pump = (Pump) row.Item;
                _pumpService.EditPump(pump.Capacity, pump.Status, pump.Id);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dgPumps.SelectedItem != null)
            {
                var selectedPump = (Pump) dgPumps.SelectedItem;

                // Delete pump:
                _pumpService.RemovePump(selectedPump.Id);

                dgPumps.ItemsSource = null;
                dgPumps.ItemsSource = _pumpService.RequestCurrentStatus(_stationId);
            }
            else
                MessageBox.Show("Select a pump first");
        }
    }
}